import APIClient from "../../network/index"

const catStore = {
	namespaced: true,
	state : {
		cateList : [],
		firstLevel : [],
		secondLevel : []
	},
	mutations : {
		Get_data(state,items){
			state.cateList = [...items];
			state.firstLevel.push({name:"所有分类"})
			items.forEach((element) => {
				if(element.pid === 0){
					state.firstLevel.push(element)
				} else {
					state.secondLevel.push(element)
				}
			})
		},
		Get_data_two(state,item){
			state.secondLevel = [];
			if(item.name === "所有分类"){
				state.cateList.forEach((element) => {
					if(element.pid != 0){
						state.secondLevel.push(element)
					}
				})
			} else {
				state.cateList.forEach((element) => {
					if(element.id === item.id){
						state.secondLevel.push(element)
					}
				})
			}
		}
	},
	actions : {
		Get_data(context){
			APIClient.ShopClassifyList().then(res => {
				console.log(res.data.data)
				context.commit("Get_data",res.data.data)
			})
		},
		Get_data_two(context,item){
			context.commit("Get_data_two",item)
		}
	}
}

export default catStore